// .........................................................................
// Title: Master_Insurance.do
//
// Overall context manager for Stata build jobs
// .........................................................................

* ---------------------------------------
* Set up environment
* ---------------------------------------

* main globals
global whoami <WHOAMI>
global code_path <CODE_PATH>
global project_path <PROJECT_PATH>

* paths
global raw "$project_path/raw"
global logs "$project_path/logs"
global tmp "$project_path/temp"
global temp "$project_path/temp"
global graphs "$project_path/graphs"
global output "$project_path/output"
global tables "$project_path/tables"
global holdings "$project_path/holdings"
global morningstar_hd "$output/morningstar_holding_detail"
global insurance_holdings "$project_path/insurance_master"

* create folders
cap mkdir $logs
cap mkdir $temp
cap mkdir $graphs
cap mkdir $output
cap mkdir $tables
cap mkdir $holdings
cap mkdir $morningstar_hd
cap mkdir $insurance_holdings

* temp folders
cap mkdir $temp/insurance
cap mkdir $temp/insurance/lists
cap mkdir $temp/insurance/life
cap mkdir $temp/insurance/life/bonds
cap mkdir $temp/insurance/life/common_equities
cap mkdir $temp/insurance/life/preferred_equities
cap mkdir $temp/insurance/life/other
cap mkdir $temp/insurance/life/loans
cap mkdir $temp/insurance/pc
cap mkdir $temp/insurance/pc/bonds
cap mkdir $temp/insurance/pc/common_equities
cap mkdir $temp/insurance/pc/preferred_equities
cap mkdir $temp/insurance/pc/other
cap mkdir $temp/insurance/pc/loans
cap mkdir $temp/insurance/health
cap mkdir $temp/insurance/health/bonds
cap mkdir $temp/insurance/health/common_equities
cap mkdir $temp/insurance/health/preferred_equities
cap mkdir $temp/insurance/health/other
cap mkdir $temp/insurance/health/loans

* log file
cap mkdir "$logs/`2'"
cap log close
if "`3'"!="" {
    log using "$logs/`2'/${whoami}_`2'_Array_`3'.log", replace
}
else {
    log using "$logs/`2'/${whoami}_`2'.log", replace
}

* ---------------------------------------
* Launch job
* ---------------------------------------

if "`2'" == "Enumerate_Insurers" {
    do $code_path/insurance/enumerate_insurers.do
}

if "`2'" == "Import_Life" {
    do $code_path/insurance/import_life.do `3'
}

if "`2'" == "Import_Health" {
    do $code_path/insurance/import_health.do `3'
}

if "`2'" == "Import_PC" {
    do $code_path/insurance/import_pc.do `3'
}

if "`2'" == "Consolidate_Insurance_Holdings" {
    do $code_path/insurance/consolidate_insurance_holdings.do
}

if "`2'" == "Industry_Aggregates" {
    do $code_path/insurance/industry_aggregates.do
}

cap log close
